package service;

import dao.OrderDao;
import model.Conn;
import model.Order;
import model.OrderItem;
import model.Page;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class OrderService {
    Order order = new Order();
    OrderDao od = new OrderDao();
    Conn dbconn = new Conn();
    Connection conn = null;

    public boolean addOrder(Order o) {
        try {
            conn = dbconn.getConn();
            //关闭自动提交
            conn.setAutoCommit(false);
            //插入订单
            od.insertOrder(conn, o);
            //获取最后插入的一个ID
            int id = od.getLastInsertId(conn);
            for (OrderItem item : o.getItemMap().values()) {
                item.getOrder().setId(id);
                od.insertOrderItem(conn, item);
            }
            //提交
            conn.commit();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            if (conn != null) {
                try {
                    conn.rollback();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            Conn.ClossAll(conn, null, null);
            return false;
        }
    }

    /**
     * 获取用户全部订单
     *
     * @param id 用户id
     * @return List<Order>
     */
    public List<Order> getAllOrder(int id) {
        List<Order> orderList = null;
        try {
            orderList = od.getAllOrderByUserId(id);
            for (Order order : orderList) {
                List<OrderItem> orderitemList = od.getAllOrderitem(order.getId());
                order.setItemList(orderitemList);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return orderList;
    }


    /**
     * 获取指定页的订单详情通过订单状态
     *
     * @param status     状态0：全部 状态1：未付款 2：已付款 3：已发货 4：已完成
     * @param pageNumber 第几页
     * @return Page
     */
    public Page selectPageByStatus(int status, int pageNumber) throws SQLException {
        Page page = new Page();
        //初始化默认一页多少数量
        int pageSize = 8;
        //设置页码
        page.setPageNumber(pageNumber);
        //对页码进行处理 好给Dao操作
        pageNumber = (pageNumber - 1) * pageSize;
        //总数量
        int totalcount;

        //有多少商品
        totalcount = od.getOrderCount(status);

        //一共好多页
        int totalPage = totalcount >= pageSize ? (int) (Math.ceil((double) totalcount / pageSize)) : 1;

        //防止用户输入的页码大于总页码
        if (page.getPageNumber() > totalPage) {
            page.setPageNumber(totalPage);
            pageNumber = (totalPage - 1) * pageSize;
        }

        //设置一页数量和总数量
        page.SetPageSizeAndTotalCount(pageSize, totalcount);
        //设置总页数
        page.setTotalPage(totalPage);

        List orderList = od.selectAllOrderByStatus(status, pageNumber, pageSize);
        for (Order order : (List<Order>) orderList) {
            int id = order.getId();
            List<OrderItem> orderItemList = od.getAllOrderitem(id);
            order.setItemList(orderItemList);
        }
        page.setList(orderList);
        return page;
    }

    /**
     * 更新订单状态
     *
     * @param ordreId 订单id
     * @param status  状态码 状态1：未付款 2：已付款 3：已发货 4：已完成 不可以为其他
     * @return boolean
     */
    public boolean updateStatus(int ordreId, int status) {
        // 规定status状态的范围
        if (status < 1 || status > 4) return false;
        boolean flag = false;
        try {
            flag = od.updateStatus(ordreId, status);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;
    }

    public boolean deleteOrder(int id) {
        try {
            conn = dbconn.getConn();
            conn.setAutoCommit(false);
            this.od.deleteOrderItem(conn, id);
            this.od.deleteOrder(conn, id);
            conn.commit();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            if (conn != null) {
                try {
                    conn.rollback();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }
        }
        Conn.ClossAll(conn, null, null);
        return false;
    }
}
